﻿using System.Threading.Tasks;
using Furion.DependencyInjection;
using Furion.InstantMessaging;
using Microsoft.AspNetCore.SignalR;
using PMP.Core.Entities;
using PMP.Core.SqlSugar;

namespace PMP.Core.SignalR;

[MapHub("/MsgHub")]
public class MsgHub : Hub,ISingleton
{
    public async Task SendProjectNotify(int toUserId, int projectId,string msg)
    {
        var model = new TbNotification();
        model.TargetUserId = toUserId;
        model.ProjectId = projectId;
        model.Message = msg;
        model.Token = SignalRToken.ProjectNotify;
        model = await DbContext.Instance.Insertable(model).ExecuteReturnEntityAsync();
        
        if(Clients == null) return;
        await Clients.User(toUserId.ToString()).SendAsync(SignalRToken.ProjectNotify, model.Id,projectId, msg);
    }
    
    public async Task SendProjectUpgradeNotify(int toUserId, int projectId,string msg)
    {
        var model = new TbNotification();
        model.TargetUserId = toUserId;
        model.ProjectId = projectId;
        model.Message = msg;
        model.Token = SignalRToken.ProjectUpgrade;
        model = await DbContext.Instance.Insertable(model).ExecuteReturnEntityAsync();
        
        if(Clients == null) return;
        await Clients.User(toUserId.ToString()).SendAsync(SignalRToken.ProjectUpgrade, model.Id,projectId, msg);
    }
    /// <summary>
    /// 人员提级消息
    /// </summary>
    /// <param name="toUserId"></param>
    /// <param name="upgradeId"></param>
    /// <param name="msg"></param>
    public async Task SendAccountUpgradeNotify(int toUserId, int upgradeId,string msg)
    {
        var model = new TbNotification();
        model.TargetUserId = toUserId;
        model.AccountUpgradeId = upgradeId;
        model.Message = msg;
        model.Token = SignalRToken.ProjectNotify;
        model = await DbContext.Instance.Insertable(model).ExecuteReturnEntityAsync();
        
        if(Clients == null) return;
        await Clients.User(toUserId.ToString()).SendAsync(SignalRToken.AccountUpgrade, model.Id,upgradeId, msg);
    }
}

public class SignalRToken
{
    /// <summary>
    /// 项目进度@
    /// </summary>
    public const string ProjectNotify = "ProjectNotify";
    /// <summary>
    /// 项目提级
    /// </summary>
    public const string ProjectUpgrade = "ProjectUpgrade";
    /// <summary>
    /// 人员提级
    /// </summary>
    public const string AccountUpgrade = "AccountUpgrade";
}